#include <QtSql>

#include "tabbuilder.h"
#include "dbconnection.h"
#include "listwidget.h"

void TabBuilder::fillTabControl(QTabWidget *tabControl, QString formName, QString parentID)
{
    tabControl->clear();
    QSqlQuery query(dbconnection::instance()->database());
    QString sql = "select fl.eventid, fl.listname, apdmclass.classname, fl.listsql "
                  "from "
                    "formlists fl left join apdmclass on fl.listsource = apdmclass.classeventid "
                  "where "
                    "fl.form='%1' and fl.parentlist is null";
    if(query.exec(sql.arg(formName)))
    {
        QSqlRecord rec = query.record();
        while(query.next())
        {
            QString whereClause(query.value(rec.indexOf("listsql")).toString());
            QString tableName = query.value(rec.indexOf("classname")).toString();
            QString headerText = query.value(rec.indexOf("listname")).toString();            
            if(!tableName.isEmpty())
            {
                addTableTab(tabControl, headerText, tableName, whereClause.arg(parentID));
            }
            else
            {
                addCustomTab(tabControl,
                             headerText,
                             query.value(rec.indexOf("eventid")).toString(),
                             parentID
                             );
            }

        }
    }
}

void TabBuilder::addTableTab(QTabWidget *tabControl, QString headerText, QString tableName, QString whereClause)
{   
    QWidget *tabNew = new QWidget();
    QGridLayout *gridLayout = new QGridLayout(tabNew);
    ListWidget *lw = new ListWidget(tabNew);
    lw->loadData(tableName, whereClause);
    gridLayout->addWidget(lw);
    tabControl->addTab(lw, headerText);
}

void TabBuilder::addCustomTab(QTabWidget *tabControl, QString headerText, QString listID, QString parentID)
{
    QWidget *tabNew = new QWidget();
    QGridLayout *gridLayout = new QGridLayout(tabNew);
    QTabWidget *widget = new QTabWidget(tabControl);
    QSqlQuery query(dbconnection::instance()->database());
    QString sql = "select fl.eventid, fl.listname, apdmclass.classname, fl.listsql "
                       "from "
                         "formlists fl left join apdmclass on fl.listsource = apdmclass.classeventid "
                       "where "
                         "fl.parentlist='%1'";
    if(query.exec(sql.arg(listID)))
    {
        QSqlRecord rec = query.record();
        while(query.next())
        {
            QString whereClause(query.value(rec.indexOf("listsql")).toString());
            QString tableName = query.value(rec.indexOf("classname")).toString();
            QString headerText = query.value(rec.indexOf("listname")).toString();
            addTableTab(widget, headerText, tableName, whereClause.arg(parentID));
        }
    }
    gridLayout->addWidget(widget);
    tabControl->addTab(tabNew, headerText);
}
